home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MOD2TXT.ZIP / CHAP7.TXT < prev    next >
Text File  |  1987-03-25  |  7KB  |  190 lines

  1.                  Chapter 7 - Overall Program Construction
  2.  
  3.  
  4.              We have pretty well covered the topic of how to put all
  5.         the  parts together to build up a program.   In this chapter
  6.         we  will go over the whole process in order to clear up  any
  7.         loose ends and have the entire process in one place.   There
  8.         is  nothing  magic  about the way  the  various  pieces  fit
  9.         together  but the rules must be followed in order to build a
  10.         usable program.
  11.  
  12.              Load and display the program named OVERPROG.MOD for the
  13.         first look at the overall structure of the Modula-2 program.
  14.         It  would  be well for you to keep in mind that there  is  a
  15.         major  category  that we have not even hinted  at  yet,  the
  16.         issue of modules.   They will be covered in Part III of this
  17.         tutorial,  and  although  they are very important,  you  can
  18.         begin writing meaningful programs before you even hear  what
  19.         modules are or how they are used.
  20.  
  21.                              NESTED PROCEDURES
  22.  
  23.              The  program  on  display contains  several  levels  of
  24.         nested  procedures  as an illustration for  you.   The  main
  25.         program has lines 1 through 37 as its declaration part,  and
  26.         lines  38  through  43 as its  statement  part.   Since  the
  27.         procedure definitions actually define the procedures  called
  28.         for  by  the  main program,  they correctly  belong  in  the
  29.         declaration part of the program.  Only two of the procedures
  30.         are  actually  callable by the main  program,  "Proc1",  and
  31.         "Proc2".   The procedure "Proc1" is a simple procedure,  but
  32.         "Proc2" has additional procedures in its declaration part.
  33.  
  34.              Procedure "Proc2" contains a declaration part in  lines
  35.         13 through 30,  and a statement part in lines 31 through 36.
  36.         Its  declaration part contains two procedures,  "Proc3"  and
  37.         "Proc4".  The nesting is carried one step farther in "Proc4"
  38.         which  contains  the  procedure "Proc5" in  its  declaration
  39.         part.  Procedures  can be nested to whatever  level  desired
  40.         according to the definition of Modula-2.
  41.  
  42.                              WHO CAN CALL WHO?
  43.  
  44.              It  is  important for you to clearly  understand  which
  45.         procedure can call which other procedures.   A procedure can
  46.         call any procedure on the same level as itself provided that
  47.         both  have  the  same parentage,  or any procedure  that  is
  48.         included in its own declaration part at the level of its own
  49.         declaration part.   For example,  the main program can  only
  50.         call  "Proc1",  and "Proc2".   The others are nested  within
  51.         "Proc2" and are not available to the main program.  Likewise
  52.         the  statement part of "Proc2" can call "Proc1",  because it
  53.         is on the same level,  and "Proc3" and "Proc4", because they
  54.         are within its declaration part.   The procedure "Proc5" can
  55.  
  56.  
  57.                                   Page 46
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                  Chapter 7 - Overall Program Construction
  68.  
  69.  
  70.         only be called by "Proc4",  because no other procedure is at
  71.         its  level.   Note  that  if  another  triple  nesting  were
  72.         included in "Proc1",  its third level procedure could not be
  73.         called  by  "Proc5"  because they would not  have  the  same
  74.         parentage.
  75.  
  76.              Nested  procedures can be very useful when you wish  to
  77.         use  a procedure that you don't want any other part  of  the
  78.         program  to  be  able  to access or  even  see.   A  private
  79.         procedure can therefore be written with no concern that  the
  80.         name may clash with some other part of the program and cause
  81.         undesirable effects.
  82.  
  83.              The  important thing to gain from this program is  that
  84.         nesting  is  possible  and  can  be  very  useful,  and  the
  85.         definition  of  a procedure is the same as that of the  main
  86.         program.   This  means that procedures can be nested  within
  87.         procedures in any way that aids in designing the program.
  88.  
  89.              Compile  and run this program and see if you understand
  90.         the output from it.
  91.  
  92.              WHERE DO WE PLACE CONSTANTS, TYPES, AND VARIABLES?
  93.  
  94.              Load  MOREPROG.MOD,  for examples of where you can  put
  95.         the other definitions in the declaration part of the program
  96.         and  the procedures.   This is a repeat of the last  program
  97.         with CONST,  TYPE, and VAR declarations added in every place
  98.         where  it is legal to put them.   This is done as an example
  99.         to  you  of  where they can be put,  so  no  explanation  of
  100.         details  will  be  given.   Some time  spent  studying  this
  101.         program  should  aid you in understanding  even  better  the
  102.         overall program construction problem.
  103.  
  104.                      WHAT ABOUT ORDER OF DECLARATIONS?
  105.  
  106.              Load the program LASTPROG.MOD for an example of how the
  107.         various fields can be ordered in the declaration part of the
  108.         program.   Notice that there are 2 procedures,  two CONST's,
  109.         two TYPE's, and two VAR's defined, but they are defined in a
  110.         seemingly  random order.   The order is random and was  done
  111.         only  to illustrate to you that the order doesn't matter  as
  112.         long as everything is defined before it is used.
  113.  
  114.              In  only one case does the order matter.   The compiler
  115.         is  very picky about where the IMPORT list goes because  the
  116.         Modula-2  language definition requires it to be  first.   In
  117.         addition, the EXPORT list must immediately follow the IMPORT
  118.         list.   We will cover both of these in detail later, for now
  119.         simply remember that the order of all declarations can  come
  120.         in  random  order as long as they follow  the  IMPORT/EXPORT
  121.  
  122.  
  123.                                   Page 47
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                  Chapter 7 - Overall Program Construction
  134.  
  135.  
  136.         lists and come before the statement part of the program.
  137.  
  138.         PROGRAMMING EXERCISES
  139.  
  140.         1.   Using the program OVERPROG,  add some calls to  illegal
  141.              places to see what messages the compiler displays.
  142.  
  143.         2.   Using  the program MOREPROG,  add some illegal variable
  144.              references to see what messages the compiler displays.
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.                                   Page 48
  190.